<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "App",
  computed: {
    ...mapState(["token",'roles'])
  },
  methods: {
    getLogin() {
      var _this = this;
      var code = _this.getUrlParam("code");

      if (!code) {
        
        let locHerf = encodeURIComponent(window.location.href);
        var url =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx68fdfbe46aff1129&redirect_uri=" +
          locHerf +
          "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        window.location.href = url;
      } else {
        if(!localStorage.getItem("code")){
          _this.login();
        }
        
      }
    },
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg); //search,查询？后面的参数，并匹配正则
      if (r != null) return unescape(r[2]);
      return "";
    },
    login() {
      let _this = this;
      var code = _this.getUrlParam("code");
      let _url = window.location.href;
      this.$ajax
        .post(`${this.$baseUrl}/login/wx`, {
          code: code,
          roles: _url.substring(_url.indexOf("roles") + 6, _url.length)
        })
        .then(function(res) {
          if (res.data.code == 200) {
            let _user = {
              img: res.data.data.head_img,
              name: res.data.data.wechat_name
            };
            localStorage.setItem("code", "1");
            localStorage.setItem('openId',res.data.data.open_id)
            localStorage.setItem('token',res.data.data.token)
            _this.$store.commit("saveToken", res.data.data.token);
            _this.$store.commit("saveRoles", res.data.data.roles);
            _this.$store.commit("saveUser", _user);
          }
        })
        .catch(function(error) {
          console.log("失败了", error);
        });
    },
     getToken() {
      let _this = this;
      let _url = window.location.href;
      console.log('获取token',_url,_url.substring(_url.indexOf("roles") + 6, _url.length))
      _this.$ajax
        .post(`${_this.$baseUrl}/login/getToken`,{
          roles:_url.substring(_url.indexOf("roles") + 6, _url.length),
          open_id:localStorage.getItem("openId")
          })
        .then(function(res) {
        
          if (res.data.code == 200) {
            let _user = {
              img: res.data.data.head_img,
              name: res.data.data.wechat_name
            };
            localStorage.setItem("code", "1");
            localStorage.setItem('token',res.data.data.token)
            _this.$store.commit("saveToken", res.data.data.token);
            _this.$store.commit("saveRoles", res.data.data.roles);
            _this.$store.commit("saveUser", _user);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    }
  },
  created() {
    // if (window.location.href.includes("roles")) {
    //   let _url = window.location.href;
    //    this.$store.commit("saveRoles", _url.substring(_url.indexOf("roles") + 6, _url.length));
       

    //   if(localStorage.getItem("openId")){
    //     this.getToken()

    //   }else{
    //     this.getLogin();

    //   }
      
    // }
  }
};
</script>

<style lang="scss">
@import "./style/common.scss";
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
.tabs {
  font-size: 0.24rem;
  background: #fff;
  padding: 0 0.3rem;
  box-sizing: border-box;
  color: #333333;
}
</style>
